<?php
class Admin_Model_Things extends Zend_Db_Table{
	protected $_name = 'things';
	protected $_primary = 'thing_id';
	
	////////////
	/* select*/
	//////////
	public function getAllThingsbyType($type)
	{// co dung
		$client = new Zend_Rest_Client(HTTP_SERVICE);
		$return = $client->restGet("/things/route/getAllThingsbyType/type/".$type);
		$return = json_decode(json_encode((array) simplexml_load_string($return->getBody())),1);
		for ($i=0;$i <count($return);$i++)
				$data[$i] = $return['row_'.$i];
				
		return $data;
	}	
	public function getThingById($thingId)
	{//có dùng
		$select = $this->select();
		$select->from('things');
		$select->setIntegrityCheck(false);
		$select->where('thing_id = ?', $thingId);
		$select->where('deleted = 0');
		$arr = $this->fetchAll($select);
		return $arr;
	}
	/*public function getAllThings()
	{
		$select = $this->select();
		$select->from('things');
		$select->setIntegrityCheck(false);
		$select->where('deleted = 0');
		$arr = $this->fetchAll($select);
		return $arr;
	}
	public function getAllThingsbyType($type)
	{
		$select = $this->select();
		$select->from('things');
		$select->setIntegrityCheck(false);
		$select->where('type = ?',$type);
		$select->where('deleted = 0');
		$arr = $this->fetchAll($select);
		return $arr;
	}	
	
	public function getThingsByCategory($category)
	{
		$select = $this->select();
		$select->from('things');
		$select->setIntegrityCheck(false);
		//$select->where('type = 1'); //1:product
		$select->where('category_id = ?', $category);
		$select->where('deleted = 0');
		$arr = $this->fetchAll($select);
		return $arr;
	}
	public function getTypedThingsByCategory($category,$type)
	{
		$select = $this->select();
		$select->from('things');
		$select->setIntegrityCheck(false);
		//$select->where('type = 1'); //1:product
		$select->where('category_id = ?', $category);
		$select->where('type = ?', $type);
		$arr = $this->fetchAll($select);
		return $arr;
	}
	
	public function getAllNews()
	{
		$select = $this->select();
		$select->from('things');
		$select->setIntegrityCheck(false);
		$select->where('type = 2'); //2:News
		$select->where('deleted = 0');
		$arr = $this->fetchAll($select);
		return $arr;
	}
	public function getAllQA()
	{
		$select = $this->select();
		$select->from('things');
		$select->setIntegrityCheck(false);
		$select->where('type = 3');
		$select->where('deleted = 0');
		$arr = $this->fetchAll($select);
		return $arr;
	}
	public function getProductsByCategory($category)
	{
		$select = $this->select();
		$select->from('things');
		$select->setIntegrityCheck(false);
		$select->where('type = 1'); //1:product
		$select->where('category_id = ?', $category);
		$select->where('deleted = 0');
		$arr = $this->fetchAll($select);
		return $arr;
	}
	public function getNewsByCategory($category)
	{
		$select = $this->select();
		$select->from('things');
		$select->setIntegrityCheck(false);
		$select->where('type = 2'); 
		$select->where('category_id = ?', $category);
		$select->where('deleted = 0');
		$arr = $this->fetchAll($select);
		return $arr;
	}
	public function getQAByCategory($category)
	{
		$select = $this->select();
		$select->from('things');
		$select->setIntegrityCheck(false);
		$select->where('type = 3'); 
		$select->where('category_id = ?', $category);
		$select->where('deleted = 0');
		$arr = $this->fetchAll($select);
		return $arr;
	}
	public function getMostViewThings($type,$limit)
	{
		$select = $this->select();
		$select->from('things');
		$select->setIntegrityCheck(false);
		$select->where('type =?',$type); //1:product, 2:new, 3:QA
		$select->where('deleted = 0');
		$select->order('views DESC');
		$select->limit($limit);
		$arr = $this->fetchAll($select);
		return $arr;
	}
	public  function getLastest($type,$limit)
	{
		$select = $this->select();
		$select->from('things');
		$select->setIntegrityCheck(false);
		$select->where('type = ?',$type);
		$select->where('deleted = 0');
		$select->Order('things.update_date DESC');
		$select->limit($limit);
		$arr = $this->fetchAll($select);
		return $arr;
	}
	public  function getLastestNewsbyshortshow( $limit)
	{
		$select = $this->select();
		$select->from('things');
		$select->setIntegrityCheck(false);
		$select->where('type = 2');
		$select->where('deleted = 0');
		$select->Order('things.thing_id DESC');
		$select->limit($limit);
		$arr = $this->fetchAll($select);
		return $arr;
	}
	public  function getLastestNews( $limit)
	{
		$select = $this->select();
		$select->from('things');
		$select->setIntegrityCheck(false);
		$select->where('type = 2');
		$select->where('deleted = 0');
		//$select->where('thing_code = "slideshow"' );
		$select->Order('things.thing_id DESC');
		$select->limit($limit);
		$arr = $this->fetchAll($select);
		return $arr;
	}
	public  function getLastestQA($limit)
	{
		$select = $this->select();
		$select->setIntegrityCheck(false);
		$select->from(array('th'=>'things'),array('title','short_description','update_date'));
		$select->join(array('u'=>'users'),'th.user_id=u.user_id');
		$select->where('th.type = 3');
		$select->where('th.deleted = 0');
		$select->Order('th.update_date DESC');
		$select->limit($limit);
		$arr = $this->fetchAll($select);
		return $arr;
	}
	public function getTypedThingById($thing_id, $type)
	{
		$select = $this->select();
		$select->from('things');
		$select->setIntegrityCheck(false);
		$select->where('type = ?', $type);
		$select->where('thing_id = ?', $thing_id);
		$select->where('deleted = 0');
		$arr = $this->fetchAll($select);
		return $arr;
	}
	public function getRelatedTypedThing($id, $cat, $type)
	{
		$select = $this->select();
		$select->from('things');
		$select->setIntegrityCheck(false);
		$select->where('type = ?', $type);
		$select->where('category_id = ?', $cat);
		$select->where('thing_id != ?', $id);
		$select->where('deleted = 0');
		$select->order('RAND()');
		$select->limit(3);
		$arr = $this->fetchAll($select);
		return $arr;
	}
	public  function getHistoryOfThing($thing_id,$limit)
	{
		$select = $this->select();
		$select->setIntegrityCheck(false);
		$select->from(array('th'=>'things'));
		$select->join(array('his'=>'history_thing'),'th.thing_code=his.thing_code');
		$select->where('th.thing_id = ?',$thing_id);
		$select->Order('th.update_date DESC');
		$select->limit($limit);
		$arr = $this->fetchAll($select);
		return $arr;
	}
	public function getMostViewQA($limit)
	{
		$select = $this->select();
		$select->setIntegrityCheck(false);
		$select->from(array('th'=>'things'),array('title','short_description','update_date','views'));
		$select->join(array('u'=>'users'),'th.user_id=u.user_id');
		$select->where('type =3'); //1:product, 2:new, 3:QA
		$select->where('deleted = 0');
		$select->order('views DESC');
		$select->limit($limit);
		$arr = $this->fetchAll($select);
		return $arr;
	}
	
	public function getProductsInPage($page,$offset)
	{
		$select = $this->select();
		$select->from('things');
		$select->setIntegrityCheck(false);
		//$select->where('thing_id = ?', $thing_id);
		$select->where('type = 1');
		$select->where('deleted = 0');
		$select->order('created_date desc');
        $select->limit($offset,($page-1)*$offset);
		$arr = $this->fetchAll($select);
		return $arr;
	}
	public function getNewsInPage($page,$offset)
	{
		$select = $this->select();
		$select->from('things');
		$select->setIntegrityCheck(false);
		//$select->where('thing_id = ?', $thing_id);
		$select->where('type = 2');
		$select->where('deleted = 0');
		$select->order('created_date desc');
        $select->limit($offset,($page-1)*$offset);
		$arr = $this->fetchAll($select);
		return $arr;
	}
	public function getThingsInPage($type,$page,$offset)
	{
		$select = $this->select();
		$select->from('things');
		$select->setIntegrityCheck(false);
		//$select->where('thing_id = ?', $thing_id);
		$select->where('type = ?', $type);
		$select->where('deleted = 0');
		$select->order('created_date desc');
        $select->limit($offset,($page-1)*$offset);
		$arr = $this->fetchAll($select);
		return $arr;
	}
	public function getThingsByCategoryInPage($category,$type,$page,$offset)
	{
		$select = $this->select();
		$select->from('things');
		$select->setIntegrityCheck(false);
		//$select->where('thing_id = ?', $thing_id);
		$select->where('type = ?', $type);
		$select->where('category_id= ?', $category);
		$select->where('deleted = 0');
		$select->order('created_date desc');
        $select->limit($offset,($page-1)*$offset);
		$arr = $this->fetchAll($select);
		return $arr;
	}
	public  function  getrandomthing ()
	{
		return null;
	}
	public function getLastThingId()
	{
		$select = $this->select();
		$select->from('things', array(new Zend_Db_Expr('max(thing_id) as maxId')));
		$select->setIntegrityCheck(false);
		$arr = $this->fetchRow($select);
		return $arr;
	}
	
	
	////////////
	/* insert
	//////////
	public function insertThings($data)
	{
		$adapter = $this->getDefaultAdapter();
		try {
			$adapter->beginTransaction();
			$adapter->insert('things', $data);
			$adapter->commit();
		}
		catch (Zend_Db_Table_Exception $e) {
			$adapter->rollBack();
			return -1;
		}
		return 1;
	}
	
	////////////
	/* update
	//////////
	public function updateThing($id,$data)
	{
		$adapter = $this->getDefaultAdapter();
		try {	
				$adapter->beginTransaction();
				$where = $adapter->quoteInto('thing_id =?', $id);
				$adapter->update('things',$data,$where);
					
				$adapter->commit();
		}
		catch (Zend_Db_Table_Exception $e) {
			$adapter->rollBack();
			return -1;
		}
		return 1;
	}*/
	
}
?>